/**
 *
 */
package rgbos.forum.webClient;

import java.util.Calendar;
import java.util.Date;
import java.util.Map;
import java.util.Vector;

import rgbos.forum.domain.Post;
import rgbos.forum.domain.Topic;
import rgbos.forum.exceptions.UserExeception;
import rgbos.forum.persistance.Persistance;

import jaxcent.HtmlInputButton;
import jaxcent.HtmlInputSubmit;
import jaxcent.HtmlInputText;
import jaxcent.HtmlPara;
import jaxcent.JaxcentPage;
import jaxcent.Jaxception;


/**
 * @author Ofir Dagan
 *
 */
public class NewMessage extends JaxcentPage {
	
	private boolean flag = false;
	private String author;
	private HtmlPara para = new HtmlPara(this, "header");
	private HtmlPara footpara_ = new HtmlPara(this,"footerPara");
	private HtmlInputText title_ = new HtmlInputText(this, "title");

	private WebClientHandler handler_ = WebClientHandler.getWebClientHandler();;
	
	private HtmlInputButton cancel = new HtmlInputButton(this, "cancel") {		
		protected void onClick( Map pageData ) {
			try {
				goBack();
			} catch (Jaxception e) {
				e.printStackTrace();
			}
		}
	};
	
	private HtmlInputSubmit submitButton = new HtmlInputSubmit(this, "newMessSubmit") {		
		protected void onClick( Map pageData ) {
			if (flag)
				return;
			flag = true;
			String title = (String) pageData.get("title");
			if (title.equals("")) {
				showMessageDialog("Please enter a title before submiting");
				flag = false;
				return;
			}
			title = handler_.escape(title);
			String body = (String) pageData.get("body");
			body = handler_.escape(body);
			final String tPath = getCurrentPath();
			final int tIndex = tPath.indexOf("topicId=");
			int topicId = Integer.valueOf(tPath.substring(tIndex+8));
			body = body.replaceAll("\n", "<br>");
			Post p = new Post(0,title,author,new Date(),body);
			if(handler_.getControl().addNewPost(p, topicId) < 0) {
				showMessageDialog("Can't edit post. Title or body too long or contain illegal characters");
				flag = false;
				return;
			}
			try {
				navigate("/showPosts.html?topicId="+topicId);
			} catch (Jaxception e) {
				e.printStackTrace();
			}
			flag = false;
		}
	};

	public NewMessage(){
		super();
	}

	protected void onUnload() {
		try {
			Map sessionData = getAllSessionData(false);
			handler_.unload(sessionData);			
		} catch (Jaxception e) {
			e.printStackTrace();
		}
	}

	protected void onLoad() {
		try {			
			final String tPath = getCurrentPath();
			final int tIndex = tPath.indexOf("topicId=");
			int topicId = Integer.valueOf(tPath.substring(tIndex+8));
			Topic top = handler_.getControl().getTopicById(topicId);
			if (top != null)
				title_.setValue("re: " + handler_.unescape(top.getTitle_()));
			setFormSaveEnabled(false);
			submitButton.setValue("Post");
			cancel.setValue("Cancel");
			Map sessionData = getAllSessionData( false );
			String type = handler_.getClientType(sessionData);
			if (type.equals("Guest")) {
				showMessageDialog("Please login or register to add posts");
				navigate("/showTopics.html");
			}				
			this.author = (String)sessionData.get("uName");		
			handler_.DisplayHeader(para, sessionData, this); //must be before display footer!
			handler_.displayFooter(footpara_, sessionData, this);
		} catch (Jaxception e) {
			e.printStackTrace();
		}
	}
}
